package cn.iocoder.yudao.module.book.service.methods;

import cn.iocoder.yudao.module.book.controller.app.protocolAndmethod.vo.AppMethodsRespVO;
import cn.iocoder.yudao.module.book.controller.app.protocolAndmethod.vo.AppProtocolRespVO;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.iocoder.yudao.module.book.controller.admin.methods.vo.*;
import cn.iocoder.yudao.module.book.dal.dataobject.methods.MethodsDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.book.dal.mysql.methods.MethodsMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.book.enums.ErrorCodeConstants.*;

/**
 * 启蒙方法 Service 实现类
 *
 * @author xi
 */
@Service
@Validated
public class MethodsServiceImpl implements MethodsService {

    @Resource
    private MethodsMapper methodsMapper;

    @Override
    public Integer createMethods(MethodsSaveReqVO createReqVO) {
        // 插入
        MethodsDO methods = BeanUtils.toBean(createReqVO, MethodsDO.class);
        methodsMapper.insert(methods);
        // 返回
        return methods.getMethodId();
    }

    @Override
    public void updateMethods(MethodsSaveReqVO updateReqVO) {
        // 校验存在
        validateMethodsExists(updateReqVO.getMethodId());
        // 更新
        MethodsDO updateObj = BeanUtils.toBean(updateReqVO, MethodsDO.class);
        methodsMapper.updateById(updateObj);
    }

    @Override
    public void deleteMethods(Integer id) {
        // 校验存在
        validateMethodsExists(id);
        // 删除
        methodsMapper.deleteById(id);
    }

    private void validateMethodsExists(Integer id) {
        if (methodsMapper.selectById(id) == null) {
            throw exception(METHODS_NOT_EXISTS);
        }
    }

    @Override
    public MethodsDO getMethods(Integer id) {
        return methodsMapper.selectById(id);
    }

    @Override
    public PageResult<MethodsDO> getMethodsPage(MethodsPageReqVO pageReqVO) {
        return methodsMapper.selectPage(pageReqVO);
    }

    @Override
    public AppMethodsRespVO getMethod() {
        System.out.println(methodsMapper.selectAll());
        return methodsMapper.selectAll();
    }

}